#
# @lc app=leetcode.cn id=206 lang=python3
#
# [206] 反转链表
#

# @lc code=start
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        # 法1：迭代求解
        if not head: return None
        pre = None
        p = head
        while p:
            p.next, pre, p = pre, p, p.next
        return pre

        # # 法2：递归求解
        # if not head or not head.next: return head
        # next_head = self.reverseList(head.next)
        # # 1 -> 2 to 2 -> 1
        # head.next.next = head
        # head.next = None
        # return next_head
            
# @lc code=end

